<template>
  <div class="storedComsumeStatistics">
    <aside-tree
      title="组织机构"
      :tree-data="treeData"
      :loading="$loadingPlugin.getTreeData"
      :mouse-button="false"
      @currentChange="currentChange"
      @refresh="getTreeData"
    />
    <div class="content">
      <el-table-form ref="form" :model="query">
        <el-form-item label="统计日期" prop="date">
          <el-date-picker
            v-model="query.date"
            type="daterange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
          />
        </el-form-item>

        <template v-slot:btns>
          <el-button type="primary" icon="el-icon-search" @click="fetchData">查询</el-button>
        </template>
      </el-table-form>

      <!-- 表格内容 -->
      <div class="table-wrapper">
        <iframe
          frameborder="0"
          width="100%"
          height="100%"
          marginheight="0"
          marginwidth="0"
          scrolling="yes"
          :src="tableDatUrl"
        />
      </div>
    </div>
  </div>
</template>

<script>
  import getTreeData from '@/utils/jsonTree'
  import { mapGetters } from 'vuex'
  import { queyrStoredComsumeStatistics } from '@/api'
  import AsideTree from '../../components/OrgTree'
  import dayjs from 'dayjs'
  export default {
    name: 'addition',
    components: {
      AsideTree
    },
    data() {
      return {
        treeData: [],
        query: {
          date: [new Date(), new Date()],
          orgSNNo: '',
          orgId: ''
        },
        tableDatUrl: ''
      }
    },
    computed: {
      ...mapGetters(['orgList'])
    },
    mounted() {
      this.getTreeData()
    },
    methods: {
      // 获取树图数据
      async getTreeData() {
        const data = this.orgList.map((item) => ({
          label: item.orgName,
          ...item
        }))
        this.treeData = getTreeData(data)
      },
      // 选择变化
      currentChange(current) {
        this.query.orgSNNo = current.orgSNNo
        this.query.orgId = current.orgId
        this.fetchData()
      },

      async fetchData() {
        const query = {
          ...this.query,
          menuId: '50404'
        }
        if (this.query.date && this.query.date.length > 0) {
          query[`startTime`] = dayjs(this.query.date[0]).format()
          query[`endTime`] = dayjs(this.query.date[1]).format()
        }
        delete query.date

        const { status, data } = await queyrStoredComsumeStatistics(query)
        if (status === 200) {
          this.tableDatUrl = data
        }
      }
    }
  }
</script>

<style lang="scss" scoped>
.storedComsumeStatistics {
  display: flex;
  // height: 630px;
  height: 100%;
  .content {
    flex: 1;
    padding: 10px 10px 0 10px;
    display: flex;
    flex-direction: column;

    .table-wrapper {
      padding-bottom: 20px;
      // height: 400px;
      flex: 1;
    }
  }
}
</style>
